<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Python 流程控制-for循环字典 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Python 流程控制-for循环字典</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-流程控制-for循环-字典"><a href="#Python-流程控制-for循环-字典" class="headerlink" title="Python 流程控制-for循环 字典"></a>Python 流程控制-for循环 字典</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python for循环不但可以遍历序列，还可以遍历字典。</p>
<h2 id="通过-for-循环遍历字典"><a href="#通过-for-循环遍历字典" class="headerlink" title="通过 for 循环遍历字典"></a>通过 for 循环遍历字典</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先定义一个字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">4</span>]: dic = &#123;<span class="string">'a'</span>:<span class="number">1</span>, <span class="string">'b'</span>:<span class="number">2</span>&#125;</div><div class="line"></div><div class="line">In [<span class="number">5</span>]: dic.fromkeys(<span class="string">'abcde'</span>,<span class="number">100</span>)</div><div class="line">Out[<span class="number">5</span>]: &#123;<span class="string">'a'</span>: <span class="number">100</span>, <span class="string">'b'</span>: <span class="number">100</span>, <span class="string">'c'</span>: <span class="number">100</span>, <span class="string">'d'</span>: <span class="number">100</span>, <span class="string">'e'</span>: <span class="number">100</span>&#125;</div><div class="line"></div><div class="line">In [<span class="number">6</span>]: dic1 = dic.fromkeys(<span class="string">'abcde'</span>,<span class="number">100</span>)</div><div class="line"></div><div class="line">In [<span class="number">7</span>]: dic1</div><div class="line">Out[<span class="number">7</span>]: &#123;<span class="string">'a'</span>: <span class="number">100</span>, <span class="string">'b'</span>: <span class="number">100</span>, <span class="string">'c'</span>: <span class="number">100</span>, <span class="string">'d'</span>: <span class="number">100</span>, <span class="string">'e'</span>: <span class="number">100</span>&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;遍历字典里的 key （直接遍历字典，不加任何方法就是遍历字典的 key）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">8</span>]: <span class="keyword">for</span> k <span class="keyword">in</span> dic:</div><div class="line">   ...:     <span class="keyword">print</span> k</div><div class="line">   ...:     </div><div class="line">a</div><div class="line">b</div><div class="line"></div><div class="line">In [<span class="number">9</span>]: <span class="keyword">for</span> k <span class="keyword">in</span> dic1:</div><div class="line">   ...:     <span class="keyword">print</span> k</div><div class="line">   ...:     </div><div class="line">a</div><div class="line">c</div><div class="line">b</div><div class="line">e</div><div class="line">d</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;取字典里的值（要遍历 value 就要加上 dic1[k] 索引下标来取）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">10</span>]: <span class="keyword">for</span> k <span class="keyword">in</span> dic1:</div><div class="line">    ...:     <span class="keyword">print</span> k,dic1[k]</div><div class="line">    ...:     </div><div class="line">a <span class="number">100</span></div><div class="line">c <span class="number">100</span></div><div class="line">b <span class="number">100</span></div><div class="line">e <span class="number">100</span></div><div class="line">d <span class="number">100</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式化的输出</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">11</span>]: <span class="keyword">for</span> k <span class="keyword">in</span> dic1:</div><div class="line">    ...:     <span class="keyword">print</span> <span class="string">"%s --&gt; %s"</span> % (k,dic1[k])</div><div class="line">    ...:     </div><div class="line">a --&gt; 100</div><div class="line">c --&gt; 100</div><div class="line">b --&gt; 100</div><div class="line">e --&gt; 100</div><div class="line">d --&gt; 100</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果只要占一行就在后边加逗号</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">12</span>]: <span class="keyword">for</span> k <span class="keyword">in</span> dic1:</div><div class="line">    ...:     <span class="keyword">print</span> <span class="string">"%s --&gt; %s"</span> % (k,dic1[k]),</div><div class="line">    ...:     </div><div class="line">a --&gt; 100 c --&gt; 100 b --&gt; 100 e --&gt; 100 d --&gt; 100</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还可以把字典作为列表遍历，取到每个元素也就是元组（使用 items() 遍历，通过一个值取出来就是元组）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">13</span>]: dic1.items()</div><div class="line">Out[<span class="number">13</span>]: [(<span class="string">'a'</span>, <span class="number">100</span>), (<span class="string">'c'</span>, <span class="number">100</span>), (<span class="string">'b'</span>, <span class="number">100</span>), (<span class="string">'e'</span>, <span class="number">100</span>), (<span class="string">'d'</span>, <span class="number">100</span>)]</div><div class="line"></div><div class="line">In [<span class="number">14</span>]: <span class="keyword">for</span> i <span class="keyword">in</span> dic1.items():<span class="keyword">print</span> i</div><div class="line">(<span class="string">'a'</span>, <span class="number">100</span>)</div><div class="line">(<span class="string">'c'</span>, <span class="number">100</span>)</div><div class="line">(<span class="string">'b'</span>, <span class="number">100</span>)</div><div class="line">(<span class="string">'e'</span>, <span class="number">100</span>)</div><div class="line">(<span class="string">'d'</span>, <span class="number">100</span>)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后可以对元组进行拆分（使用 items(),通过两个值来取就可以拆分元组）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">15</span>]: <span class="keyword">for</span> k,v <span class="keyword">in</span> dic1.items():<span class="keyword">print</span> k,v</div><div class="line">a <span class="number">100</span></div><div class="line">c <span class="number">100</span></div><div class="line">b <span class="number">100</span></div><div class="line">e <span class="number">100</span></div><div class="line">d <span class="number">100</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以用 dic1.iteritems() ，只不过它返回是个对象；而 items() 是个列表。可以对这个对象进行遍历，用两个值（k和v）来接收，就可以得到两个值 key 和 value</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">16</span>]: <span class="keyword">for</span> k,v <span class="keyword">in</span> dic1.iteritems():<span class="keyword">print</span> k,v</div><div class="line">a <span class="number">100</span></div><div class="line">c <span class="number">100</span></div><div class="line">b <span class="number">100</span></div><div class="line">e <span class="number">100</span></div><div class="line">d <span class="number">100</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上就是通过 for 循环遍历字典的方法</p>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><h3 id="实例1：乘法口诀表"><a href="#实例1：乘法口诀表" class="headerlink" title="实例1：乘法口诀表"></a>实例1：乘法口诀表</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两个循环</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> xrange(<span class="number">1</span>,<span class="number">10</span>):    <span class="comment">#控制行数</span></div><div class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> xrange(<span class="number">1</span>,i+<span class="number">1</span>):    <span class="comment">#每列的列数不一样，用j表示，控制列数</span></div><div class="line">        <span class="keyword">print</span> <span class="string">"%s * %s = %s"</span> % (j, i j*i),    <span class="comment">#用逗号不输出换行符</span></div><div class="line">    <span class="keyword">print</span>    <span class="comment">#直接print输出换行,或者使用 print "\n"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># python 3.py </span></div><div class="line"><span class="number">1</span>*<span class="number">1</span>=<span class="number">1</span></div><div class="line"><span class="number">1</span>*<span class="number">2</span>=<span class="number">2</span> <span class="number">2</span>*<span class="number">2</span>=<span class="number">4</span></div><div class="line"><span class="number">1</span>*<span class="number">3</span>=<span class="number">3</span> <span class="number">2</span>*<span class="number">3</span>=<span class="number">6</span> <span class="number">3</span>*<span class="number">3</span>=<span class="number">9</span></div><div class="line"><span class="number">1</span>*<span class="number">4</span>=<span class="number">4</span> <span class="number">2</span>*<span class="number">4</span>=<span class="number">8</span> <span class="number">3</span>*<span class="number">4</span>=<span class="number">12</span> <span class="number">4</span>*<span class="number">4</span>=<span class="number">16</span></div><div class="line"><span class="number">1</span>*<span class="number">5</span>=<span class="number">5</span> <span class="number">2</span>*<span class="number">5</span>=<span class="number">10</span> <span class="number">3</span>*<span class="number">5</span>=<span class="number">15</span> <span class="number">4</span>*<span class="number">5</span>=<span class="number">20</span> <span class="number">5</span>*<span class="number">5</span>=<span class="number">25</span></div><div class="line"><span class="number">1</span>*<span class="number">6</span>=<span class="number">6</span> <span class="number">2</span>*<span class="number">6</span>=<span class="number">12</span> <span class="number">3</span>*<span class="number">6</span>=<span class="number">18</span> <span class="number">4</span>*<span class="number">6</span>=<span class="number">24</span> <span class="number">5</span>*<span class="number">6</span>=<span class="number">30</span> <span class="number">6</span>*<span class="number">6</span>=<span class="number">36</span></div><div class="line"><span class="number">1</span>*<span class="number">7</span>=<span class="number">7</span> <span class="number">2</span>*<span class="number">7</span>=<span class="number">14</span> <span class="number">3</span>*<span class="number">7</span>=<span class="number">21</span> <span class="number">4</span>*<span class="number">7</span>=<span class="number">28</span> <span class="number">5</span>*<span class="number">7</span>=<span class="number">35</span> <span class="number">6</span>*<span class="number">7</span>=<span class="number">42</span> <span class="number">7</span>*<span class="number">7</span>=<span class="number">49</span></div><div class="line"><span class="number">1</span>*<span class="number">8</span>=<span class="number">8</span> <span class="number">2</span>*<span class="number">8</span>=<span class="number">16</span> <span class="number">3</span>*<span class="number">8</span>=<span class="number">24</span> <span class="number">4</span>*<span class="number">8</span>=<span class="number">32</span> <span class="number">5</span>*<span class="number">8</span>=<span class="number">40</span> <span class="number">6</span>*<span class="number">8</span>=<span class="number">48</span> <span class="number">7</span>*<span class="number">8</span>=<span class="number">56</span> <span class="number">8</span>*<span class="number">8</span>=<span class="number">64</span></div><div class="line"><span class="number">1</span>*<span class="number">9</span>=<span class="number">9</span> <span class="number">2</span>*<span class="number">9</span>=<span class="number">18</span> <span class="number">3</span>*<span class="number">9</span>=<span class="number">27</span> <span class="number">4</span>*<span class="number">9</span>=<span class="number">36</span> <span class="number">5</span>*<span class="number">9</span>=<span class="number">45</span> <span class="number">6</span>*<span class="number">9</span>=<span class="number">54</span> <span class="number">7</span>*<span class="number">9</span>=<span class="number">63</span> <span class="number">8</span>*<span class="number">9</span>=<span class="number">72</span> <span class="number">9</span>*<span class="number">9</span>=<span class="number">81</span></div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/2.3 流程控制-for 字典/Python 流程控制-for循环 字典/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
